package com.code.day01.Prepered;

import java.sql.*;
import java.util.Scanner;

@SuppressWarnings("all")
public class JDBCWork {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名与密码");
        String username = sc.nextLine();
        String password = sc.nextLine();

        // 连接信息
        String url = "jdbc:mysql://192.168.254.134:3306/msgcenter";
        String user = "root";
        String sqlpassword = "esz8311l";

        Connection connection = DriverManager.getConnection(url, user, sqlpassword);

        String sql = "select * from user where username = ? and password = ?";
        String insert = "insert into user (username, password) values (?, ?)";

        PreparedStatement pst = connection.prepareStatement(sql);
        PreparedStatement pst1 = connection.prepareStatement(insert);

        pst.setString(1, username);
        pst.setString(2, password);

        ResultSet rs = pst.executeQuery();

        if (rs.next()) {
            System.out.println("已存在该用户, 注册失败");
        } else {
            pst1.setString(1, username);
            pst1.setString(2, password);
            pst1.executeUpdate();
            System.out.println("注册成功");
        }

        rs.close();
        pst1.close();
        pst.close();
        connection.close();
    }
}
